﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace BGSpaceShooter
{
    public partial class ServerAddress : Form
    {
        public string IpAddress { get; private set; }
        public int Port { get; private set; }

        public ServerAddress()
        {
            InitializeComponent();
        }

        private void connectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                Port = int.Parse(portBox.Text);
                IpAddress = addressBox.Text;
                if (IpAddress.Split('.').Count() != 4)
                    throw new Exception("Bad IP");
                Ping ping = new Ping();
                if (ping.Send(IPAddress.Parse(IpAddress)).Status != IPStatus.Success)
                    throw new ArgumentException("Host niedostepny");

                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Błędnie wprowadzone dane");
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
